<?php
namespace app\index\model;
use think\Model;

/**
 * Class Redis
 * @package App\index\model
 */
class Redis extends Model{
    // Redis对象
    private static $obj;

    // Redis地址
    private static $host = '127.0.0.1';
    private static $port = 6379;

    // Redis密码
    private static $pwd = '';

    //--------------------------------------------
    //  创建Redis对象
    //--------------------------------------------

    private static function obj()
    {
        if (is_null(self::$obj)) {
            self::$obj = new \Redis();
            self::$obj->connect(self::$host, self::$port);
            is_null(self::$pwd) || self::$obj->auth(self::$pwd);
        }
        return self::$obj;
    }

    //--------------------------------------------
    //  Redis增删改查
    //--------------------------------------------

    static function __callStatic($name, $args)
    {
        $redis = self::obj();
        if (method_exists($redis, $name)) {
            return call_user_func_array([$redis, $name], $args);
        }
    }

    // 检查是否存在key值
    static function key_exists($key)
    {
        $redis = self::obj();
        return $redis->exists($key) ? true : false;
    }

    //--------------------------------------------
    //  关闭Redis链接
    //--------------------------------------------

    static function close()
    {
        is_null(self::$obj) || self::obj()->close();
    }
}